package com.gbb.simple.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.gbb.simple.base.BaseFragmentActivity;
import com.gbb.common.adapter.AbstractAdapter;
import com.gbb.simple.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gaobingbing
 */
public class DemoListActivity extends BaseFragmentActivity implements OnClickListener {

    private DemoAdapter demoAdapter;

    private List<String> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_list);

        dataList = new ArrayList<>();
        for(int i = 1; i <= 100; i++) {
            dataList.add("测试" + i);
        }

        demoAdapter = new DemoAdapter(mContext,dataList);
        ListView demoListView = (ListView)findViewById(R.id.listview_demo);
        demoListView.setAdapter(demoAdapter);

        findViewById(R.id.btn_list_add).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {

            case R.id.btn_list_add:
                dataList.add("New Item");
                demoAdapter.setDataList(dataList);
                break;

            default:
                break;
        }
    }

    private class DemoAdapter extends AbstractAdapter<String> {

        private DecimalFormat dFormat = new DecimalFormat("#0");

        public DemoAdapter(Context mContext,List<String> dataList) {
            super(mContext,dataList);
        }

        @Override
        public View getView(int position,View convertView,ViewGroup parent) {
            if(convertView == null) {
                Log.d("gbb","convertView == null");
                convertView = mInflater.inflate(android.R.layout.simple_list_item_1,null);
                Log.d("gaobb","getView_" + dFormat.format(position) + "," + convertView);
            }
            TextView textView = (TextView)convertView.findViewById(android.R.id.text1);
            textView.setText(getItem(position));
            return convertView;
        }

    }

}
